/**
 * Created by zhang_yao_wu1993 on 15/4/21.
 */
var inputNames = [
    "username",
    "password",
    "password_confirm",
    "nickname",
    "qq",
    "email"
];

var regexp = [
    /^[A-Za-z0-9]{6,21}/,
    /^[A-Za-z0-9]{6,21}/,
    /^[A-Za-z0-9]{6,21}/,
    /^.{2,21}/,
    /^[0-9]{4,12}$/,
    /^[A-Za-z0-9_]{2,21}@[A-Za-z]{2,5}\.[A-Za-z]{2,5}$/
];

var checkTable = [
    false,
    false,
    false,
    false,
    false,
    false
];

$(document).ready(function() {
    // 初始化背景
    $("#particles").particleground({
        dotColor: "#5cbdaa",
        lineColor: "#5cbdaa"
    });
    
    $(".intro").css({
        "margin-top": -($(".intro").height() / 2)
    });

    // 绑定表单验证
    for(var i = 0; i < inputNames.length; i++) {
        formBindInputToCheckValid(i, inputNames[i]);
    }

    function formBindInputToCheckValid(inputIndex, inputName) {
        $("#register_box form div div input[name = '" + inputName + "']").bind("input", function() {
            if (regexp[inputIndex].test($("#register_box form div div input[name = '" + inputName + "']").val())) {
                checkTable[inputIndex] = true;
                changeSuccessFlag($("#register_box form div div input[name = '" + inputName + "']"), true);
                enableSubmitButtonIfCheckTableAllTrue();
            } else {
                checkTable[inputIndex] = false;
                changeSuccessFlag($("#register_box form div div input[name = '" + inputName + "']"), false);
                disableSubmitButton();
            }
        });
    }

    function enableSubmitButtonIfCheckTableAllTrue() {
        for(var i = 0; i < checkTable.length; i++) {
            if(checkTable[i] == false) {
                return;
            }
        }
        $("#submit_button").removeClass("disabled");
    }

    function disableSubmitButton() {
        $("#submit_button").addClass("disabled");
    }

    function changeSuccessFlag(element, successFlag) {
        if(successFlag == true) {
            element.parent().children("div").children("i").removeClass("remove circle");
            element.parent().children("div").children("i").addClass("check circle");
        } else {
            element.parent().children("div").children("i").removeClass("check circle");
            element.parent().children("div").children("i").addClass("remove circle");
        }
    }

    $("#submit_button").click(function() {
        $("#register_box form").submit();
    });
});